package cn.xiehang.SoundSight;

import android.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.List;

public class CartAdapter extends RecyclerView.Adapter<CartAdapter.CartViewHolder> {
    private List<Product> productList;
    private OnItemClickListener listener;
    private boolean isSelectAll = false;

    public interface OnItemClickListener {
        void onSelectChange(int position, boolean isSelected);
        void onQuantityChange(int position, int quantity);
    }

    public CartAdapter(List<Product> productList, OnItemClickListener listener) {
        this.productList = productList;
        this.listener = listener;
    }

    @NonNull
    @Override
    public CartViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_cart, parent, false);
        return new CartViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CartViewHolder holder, int position) {
        Product product = productList.get(position);

        // 绑定数据
        holder.tvProductName.setText(product.getName());
        holder.tvProductPrice.setText(String.format("¥%.2f", product.getPrice()));
        holder.tvQuantity.setText(String.valueOf(product.getBuyCount()));
        holder.cbSelect.setChecked(product.isSelected());

        // 使用Glide加载图片
        Glide.with(holder.itemView.getContext())
                .load(product.getImageUrl())
                .placeholder(R.drawable.shopping_mall_iamge)
                .into(holder.ivProduct);

        // 选择按钮点击事件
        holder.cbSelect.setOnCheckedChangeListener((buttonView, isChecked) -> {
            product.setSelected(isChecked);
            if (listener != null) {
                listener.onSelectChange(position, isChecked);
            }
        });

        // 减少数量按钮
        holder.btnDecrease.setOnClickListener(v -> {
            int buyCount = product.getBuyCount();
            if (buyCount > 1) {
                buyCount--;
                product.setBuyCount(buyCount);
                holder.tvQuantity.setText(String.valueOf(buyCount));
                if (listener != null) {
                    listener.onQuantityChange(position, buyCount);
                }
            } else {
                productList.remove(position);
                notifyItemRemoved(position);
                notifyItemRangeChanged(position, productList.size());
                if (listener != null) {
                    listener.onQuantityChange(position, 0);
                }
            }
        });

        // 增加数量按钮
        holder.btnIncrease.setOnClickListener(v -> {
            int buyCount = product.getBuyCount()+1;
            product.setBuyCount(buyCount);
            holder.tvQuantity.setText(String.valueOf(buyCount));
            if (listener != null) {
                listener.onQuantityChange(position, buyCount);
            }
        });

        // 删除按钮
        holder.Delete.setOnClickListener(v -> {
            new AlertDialog.Builder(holder.itemView.getContext())
                    .setTitle("删除商品")
                    .setMessage("确定要删除该商品吗？")
                    .setPositiveButton("确定", (dialog, which) -> {
                        productList.remove(position);
                        notifyItemRemoved(position);
                        notifyItemRangeChanged(position, productList.size());
                        if (listener != null) {
                            listener.onQuantityChange(position, 0);
                        }
                    })
                    .setNegativeButton("取消", null)
                    .show();
        });
    }

    @Override
    public int getItemCount() {
        return productList.size();
    }

    public void selectAll(boolean isSelectAll) {
        this.isSelectAll = isSelectAll;
        for (Product product : productList) {
            product.setSelected(isSelectAll);
        }
        notifyDataSetChanged();
    }

    static class CartViewHolder extends RecyclerView.ViewHolder {
        CheckBox cbSelect;
        ImageView ivProduct;
        TextView tvProductName;
        TextView tvProductPrice;
        Button btnDecrease;
        TextView tvQuantity;
        Button btnIncrease;
        Button Delete;

        public CartViewHolder(@NonNull View itemView) {
            super(itemView);
            cbSelect = itemView.findViewById(R.id.cb_select);
            ivProduct = itemView.findViewById(R.id.iv_product);
            tvProductName = itemView.findViewById(R.id.tv_product_name);
            tvProductPrice = itemView.findViewById(R.id.tv_product_price);
            btnDecrease = itemView.findViewById(R.id.btn_decrease);
            tvQuantity = itemView.findViewById(R.id.tv_quantity);
            btnIncrease = itemView.findViewById(R.id.btn_increase);
            Delete = itemView.findViewById(R.id.btn_delete);

        }
    }

    //更新存储数据的数量

}